<template>
    <div class="mod-role" style="padding: 20px 10px;">
      <el-form :inline="true"  @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="pages.name" placeholder="接口名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList(1)">查询</el-button>
          <el-button  type="primary" @click="addOrUpdateHandle({'add':true})">新增</el-button>
          <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;">
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          prop="interfaceName"
          header-align="center"
          align="center"
          label="接口名称">
        </el-table-column>
        <el-table-column
          prop="name"
          header-align="center"
          align="center"
          show-overflow-tooltip
          label="接口描述">
        </el-table-column>
        <el-table-column
          prop="url"
          header-align="center"
          align="center"
          show-overflow-tooltip
          label="地址">
        </el-table-column>
        <el-table-column
          prop="method"
          header-align="center"
          align="center"
          width="170"
          label="请求方式">
        </el-table-column>
        <el-table-column
          prop="dataPath"
          header-align="center"
          align="center"
          label="参数位置">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="170"
          label="操作">
          <template slot-scope="scope">
            <el-button  type="text" size="small" @click="addOrUpdateHandle(scope.row)">修改</el-button>
            <el-button  type="text" size="small" @click="configEdit(scope.row.id)">配置</el-button>
            <el-popconfirm title="确定删除吗？" icon-color="#f00" @confirm="sendRquestDelete([scope.row.id])">
              <el-button slot="reference" type="text" size="small">删除</el-button>
          </el-popconfirm>
            <el-button @click="verification(scope.row)" type="text" size="small">验证</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="padding-top: 10px;text-align: right;">
        <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>

      <el-dialog
      width="45%"
      :title="titleStr"
      :visible.sync="editPostJsonFlag"
      :close-on-click-modal="false"
      append-to-body>
      <div v-if="editPostJsonFlag">
        <vue-json-editor
          v-model="resultInfo"
          :showBtns="false"
          :mode="'code'"
          @json-change="onJsonChange"
          @json-save="onJsonSave"
          @has-error="onError"
        />
        <br>
      <el-button :disabled="showloading" :type="!showloading?'primary':'success'" @click="checkJson">
        {{ !showloading?'确定':'等待……' }}
      </el-button>
      <div v-if="resultInfo2!=''" style="max-height:250px;overflow-y: scroll;border:2px solid #eee;margin:10px 0 0 0 ">
        <pre> {{ resultInfo2 }}</pre>
      </div>
    </div>
    </el-dialog>

    <el-dialog
      width="70%"
      title="配置"
      :visible.sync="configFlag"
      :close-on-click-modal="false"
    >
    <div style="text-align: right;"> <i @click="add" style="font-size:26px;color:#17B3A3;margin-left:12px;" class="el-icon-circle-plus"></i></div>
    <el-table
      :data="tableListData"
      border
      style="width: 100%">
      <el-table-column
        prop="realapiId"
        label="ID"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="type"
        align="center"
        label="转换类型"
        width="180">
        <template slot-scope="scope">
          <el-select @change="changeType(scope.$index,scope.row.type)" v-model="scope.row.type" placeholder="请选择">
            <el-option
              v-for="(item,index) in options1"
              :key="index"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        prop="regix"
        align="center"
        label="转化原始值">
        <template slot-scope="scope">
          <el-input v-model="scope.row.title" placeholder="请输入内容"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="title"
        align="center"
        label="键值">
        <template slot-scope="scope">
          <el-input v-model="scope.row.keyName" placeholder="请输入内容"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="value"
        align="center"
        label="转换值">
        <template slot-scope="scope">
          <el-input v-if="scope.row.type!='字段脱敏'"  v-model="scope.row.value" placeholder="请输入内容"></el-input>
          <el-select @change="setRowVal(scope.$index,scope.row.value)" v-else v-model="scope.row.value" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.maskingName"
              :value="item.maskingName">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        prop="target"
        align="center"
        label="脱敏正则">
        <template slot-scope="scope">
          <el-input :disabled="scope.row.type=='字段脱敏'" v-model="scope.row.regix" placeholder="请输入内容"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="keyName"
        align="center"
        label="脱敏">
        <template slot-scope="scope">
          <el-input :disabled="scope.row.type=='字段脱敏'" v-if="scope.row.type=='字段脱敏'" v-model="scope.row.target" placeholder="请输入内容"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop=""
        align="center"
        width="80"
        label="操作">
        <template slot-scope="scope">
          <el-button @click="deleteRow(scope.$index)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: right;margin-top: 10px;">
      <el-button @click="cancellation">取消</el-button>
      <el-button @click="commitFromData" type="primary" >确定</el-button>
    </div>
    </el-dialog>
    </div>
  </template>


<script>
    import request from '@/utils/request'
    import vueJsonEditor from 'vue-json-editor'
    import AddOrUpdate from './role-add-or-update'
    export default {
      data () {
        return {
          options1:[],
          options:[],
          tableListData:[],
          configFlag:false,
          showloading:false,
          queryStr:"",
          titleStr:"JSON参数验证",
          resultInfo2:"",
          editPostJsonFlag: false,
          resultInfo:{},
          dataList: [],
          pageIndex: 1,
          pageSize: 10,
          hasJsonFlag:true,  // json是否验证通过
          totalPage: 0,
          dataListLoading: false,
          dataListSelections: [],
          addOrUpdateVisible: false,
          selectId:'',
          pages:{
            "name": "",
            "page": 1,
            "limit": 10
          }
        }
      },
      components: {
        AddOrUpdate,vueJsonEditor
      },
      created () {
        request({
            url:'/ht-datadevelop/decryption/getDataList',
            method: 'get'
          }).then((res) => {
            this.options = res.data
          })
          request({
            url: '/ht-datadevelop/api/real/manage/getConversionKey',
            method: 'post',
            data:{"key":"conversion"}
          }).then((res) => {
            this.options1 = res.dataList
          })
        this.getDataList()
      },
      methods: {
        changeType(i,v){
          if(v=='字段脱敏'){
            let p = JSON.parse(JSON.stringify(this.tableListData))  
            p[i].value = ''
            p[i].regix = ''
            p[i].target = ''
            this.tableListData = JSON.parse(JSON.stringify(p))
          }
        },
        setRowVal(index,val){
          let p = JSON.parse(JSON.stringify(this.tableListData))
          this.options.forEach((item,count)=>{
            if(item.maskingName == val){
              p[index].regix = item.maskingRegix
              p[index].target = item.maskingTarget
            }
          })
          this.tableListData = JSON.parse(JSON.stringify(p))
        },
        commitFromData(){
          console.log(this.tableListData)
          request({
            url: '/ht-datadevelop/api/real/manage/saveOrUpdateConversion',
            method: 'post',
            data:{dataList:this.tableListData}
          }).then((res) => {
            if(res.code==200){
              this.$message.success('保存成功')
              this.cancellation()
            }else{
              this.$message.success(res.msg)
            }
          })
        },
        cancellation(){
          this.configFlag = false
          this.tableListData = []
        },
        add(){
          this.tableListData.push({
            "realapiId":this.selectId,
            "type": "",
            "keyName": "",
            "title": "",
            "value": "",
            "regix": "",
            "target": ""
          })
        },
        deleteRow(i){
          this.tableListData.splice(i,1)
        },
        configEdit(v){
          this.selectId = v
          request({
            url:'/ht-datadevelop/api/real/manage/getConversionList',
            method: 'post',
            data: {realapiId:v}
          }).then((res) => {
            this.tableListData = res.data
            this.tableListData.forEach((item,index)=>{
              item.realapiId = v
            })
          })
          this.configFlag = true
        },
        onJsonChange (value) {
          this.onJsonSave(value)
        },
        onJsonSave (value) {
          this.resultInfo = value
          this.hasJsonFlag = true
        },
        onError(value) {
          this.hasJsonFlag = false
        },
        verification(v){
          this.resultInfo = {}
          this.queryStr = v.interfaceName
          this.resultInfo2 =  ''
          this.editPostJsonFlag = true
        },
              // 检查json
      checkJson(){
        this.showloading = true
        request({
            url:'/ht-datadevelop/api/real/manage/asyncRequestApi/'+this.queryStr,
            method: 'post',
            data: this.resultInfo
          }).then((res) => {
            this.showloading = false
            if(res.code==200){
              this.resultInfo2 = res.data.dataList||'无数据'
            }
          })
          setTimeout(()=>{
            this.showloading = false
          },60*1000)
      },
        // 获取数据列表
        getDataList (v) {
          this.pages.page = v=='1'?'1':this.pages.page+''
          this.pages.limit = this.pages.limit+''
          this.dataListLoading = true
          request({
            url:'/ht-datadevelop/api/real/manage/list',
            method: 'post',
            data: this.pages
          }).then((res) => {
            if (res.code === 200) {
              this.dataList = res.data.list
              this.totalPage = res.data.totalCount
            } else {
              this.dataList = []
              this.totalPage = 0
            }
            this.dataListLoading = false
          })
        },
        // 每页数
        sizeChangeHandle (val) {
          this.pages.limit = val
          this.pageIndex = 1
          this.getDataList()
        },
        // 当前页
        currentChangeHandle (val) {
          this.pages.page = val
          this.getDataList()
        },
        // 多选
        selectionChangeHandle (val) {
          this.dataListSelections = val
        },
        // 新增 / 修改
        addOrUpdateHandle (row) {
          this.addOrUpdateVisible = true
          this.$nextTick(() => {
            this.$refs.addOrUpdate.init(row)
          })
        },
        sendRquestDelete(ids){
          request({
              url:'/ht-datadevelop/api/real/manage/deleteByIds',
              method: 'post',
              data:{ids:ids}
            }).then((res) => {
              if (res.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.getDataList()
                  }
                })
              } else {
                this.$message.error(res.msg)
              }
            })
        },
        // 删除
        deleteHandle (id) {
          var ids =  this.dataListSelections.map(item => {
            return item.id
          })
          this.$confirm(`确定${id ? '删除' : '批量删除'}?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.sendRquestDelete(ids)
          }).catch(() => {})
        }
      }
    }
  </script>


<style lang="scss" scoped>
.editContent{
  border-radius: 10px;
  padding:10px;
  border:1px solid #eee;
  >div{
    max-height: 300px;
    overflow-y: scroll;
    .p2{
      display: flex;
      flex-direction: row;
      margin: 5px 0 3px 0;
      .el-input{
        flex: 1;
        box-sizing: border-box;
        border: 1px solid #fff;
        margin-right: 12px;
      }
      // span{
      //   position: relative;
      //   top:5px;
      //   margin-left:16px;
      //   i{
      //     cursor: pointer;
      //   }
      // }
    }
  }
  .p1{
    margin: 0;
    display: flex;
    flex-direction: row;
    span{
      display: inline-block;
      flex: 1;
      text-align: center;
      background: #eee;
      font-weight: bold;
      padding: 0 5px;
      border-right: 1px solid #fff;
    }
  }
}
</style>







